package com.snake;

import com.snake.bean.HfGoods;
import com.snake.db.Demo;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;

import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Administrator on 2018/5/21 0021.
 */
public class TestSnake {
    private static WebDriver driver;
    private static StringBuffer verificationErrors = new StringBuffer();

    public static void main(String[] args) throws InterruptedException {
        String browserDriverUrl = "E:\\chromedriver\\chromedriver.exe";// 浏览器驱动路径
        //启动chrome浏览器
        System.setProperty("webdriver.chrome.driver", browserDriverUrl);
        //取消 chrome正受到自动测试软件的控制的信息栏
        ChromeOptions options = new ChromeOptions();
        options.addArguments("disable-infobars");
        DesiredCapabilities capabilities = DesiredCapabilities.chrome();
        capabilities.setCapability("chrome.switches", Arrays.asList("--start-maximized"));
        options.addArguments("--test-type", "--start-maximized");

        driver = new ChromeDriver(options);
        //driver.manage().window().maximize();// 最大化浏览器
        //driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);//设置操作超时时长，该设置是全局性的，即所有操作都最长等待30s

        Thread.sleep(15000);
        String goodUrl = "https://item.taobao.com/item.htm?id=556907194294";
        driver.get(goodUrl);
        Thread.sleep(15000);
        System.out.println("----------------------------------------------------------");
        System.out.println(driver.getPageSource());
        System.out.println("----------------------------------------------------------");
           /* Actions action = new Actions(driver);
            action.keyDown(Keys.CONTROL).sendKeys("w").keyUp(Keys.CONTROL).sendKeys(Keys.NULL).perform();*/
        boolean xiajia = false;// driver.getPageSource().contains("此宝贝已下架");
        boolean sorry = false; //driver.getPageSource().contains("抱歉！页面无法访问");
        if (xiajia || sorry) {

        } else {

            //商品标题
            By jTitle = By.className("tb-main-title");
            String jTitleText = driver.findElement(jTitle).getText();

            //商品主图
            By jImgBooth = By.id("J_ImgBooth");
            String jImgBoothSrc = driver.findElement(jImgBooth).getAttribute("src");


            //商品价格 begin
            By jPromoPriceNum = By.id("J_PromoPriceNum");
            String taobaoPrice = "";
            int intFinalTaobaoPrice = 0;
            if (WebDriverUtil.elementExist(driver, jPromoPriceNum)) {
                WebElement jPromoPriceNumElement = driver.findElement(jPromoPriceNum);
                taobaoPrice = jPromoPriceNumElement.getText();
                taobaoPrice = taobaoPrice.substring(1, taobaoPrice.length());
                intFinalTaobaoPrice = (int) Double.parseDouble(taobaoPrice) * 100;
            } else {
                By jStrPrice = By.id("J_StrPrice");
                WebElement jStrPriceElement = driver.findElement(jStrPrice);
                if (jStrPriceElement != null) {
                    taobaoPrice = jStrPriceElement.getText();
                    taobaoPrice = taobaoPrice.substring(1, taobaoPrice.length());
                    intFinalTaobaoPrice = (int) Double.parseDouble(taobaoPrice) * 100;
                }
            }
            //商品价格 end

            //店铺名称 begin
            By tbSellerName = By.className("tb-seller-name");
            String tbSellerNameText = "";
            if (WebDriverUtil.elementExist(driver, tbSellerName)) {
                WebElement tbSellerNameElement = driver.findElement(tbSellerName);
                tbSellerNameText = tbSellerNameElement.getText();
            }
            //店铺名称 end

            //商品尺码
            StringBuffer size = new StringBuffer();
            By sizeAll = By.cssSelector("#J_isku > div > dl.J_Prop.J_TMySizeProp.tb-prop.tb-clear.J_Prop_measurement");
            if (WebDriverUtil.elementExist(driver, sizeAll)) {
                By spanBy = By.tagName("span");
                List<WebElement> sizeWebElementList = driver.findElement(sizeAll).findElements(spanBy);
                for (WebElement element : sizeWebElementList
                        ) {
                    size.append(element.getText());
                    size.append(";");
                    System.out.println("有尺码==>" + element.getText());
                }
            }



            //颜色分类
            StringBuffer color = new StringBuffer();
            By colorAll = By.cssSelector(" #J_isku > div > dl.J_Prop.tb-prop.tb-clear.J_Prop_Color");
            if (WebDriverUtil.elementExist(driver, colorAll)) {
                By spanBy = By.tagName("span");
                List<WebElement> webElementList = driver.findElement(colorAll).findElements(spanBy);
                for (WebElement webElement : webElementList
                        ) {
                    color.append(webElement.getText());
                    color.append(";");
                    System.out.println("有颜色==>" + webElement.getText());
                }
            }

            //商品详情
            By goodDetail = By.id("J_SubWrap");
            Document pageSource = Jsoup.parse(driver.getPageSource());
            String goodDetailHTML = pageSource.selectFirst("#description").outerHtml();
            //System.out.println();
            //String goodDetailHtml = driver.findElement(goodDetail)
            //颜色太复杂
            //    By colorButton = By.className("J_TSaleProp tb-img tb-clearfix");
            //   String colorButtonText = driver.findElement(colorButton).getText();

            System.out.println("淘宝商品标题==>" + jTitleText);
            System.out.println("淘宝主图==>" + jImgBoothSrc);
            System.out.println("尺码==>" + size);
            System.out.println("颜色==>" + color);
            System.out.println("价格===>" + taobaoPrice);
            System.out.println("商品详情html代码===>" + goodDetailHTML);
            // System.out.println(colorButtonText);
            HfGoods hfGoods = new HfGoods();
            String[] urlParamters = goodUrl.split("&");
            long taobaoGoodId = 0;
            for (int i = 0; i < urlParamters.length; i++) {
                System.out.println("参数==>" + urlParamters[i]);
                if (urlParamters[i].contains("id")) {
                    taobaoGoodId = Long.valueOf(urlParamters[i].split("=")[1]);
                    System.out.println("=====>" + taobaoGoodId);
                }

            }
        }

    }
}
